#! python3
# combinePdfs.py - Combines all the PDFs in the current working directory into 
# a single PDF.

import PyPDF2, os

# Get all the PDF filenames.
pdfFiles = []
# Loop through every file in the specified directory.
pdfDir = os.path.join(os.getcwd(), 'materials', 'pdf')
for filename in os.listdir(pdfDir):
    if filename.endswith('.pdf'):
        pdfFiles.append(os.path.join(pdfDir, filename))
pdfFiles.sort()

pdfWriter = PyPDF2.PdfWriter()

# Loop through all the PDF files.
for filename in pdfFiles:
    pdfFileObj = open(filename, 'rb')
    pdfReader = PyPDF2.PdfReader(pdfFileObj)

    # Loop through all the pages (except the first) and add them.
    for pageNum in range(len(pdfReader.pages)):
        pageObj = pdfReader.pages[pageNum]
        pdfWriter.add_page(pageObj)

# Save the resulting PDF to a file.
# pdfOutput = open('allminutes.pdf', 'wb')
pdfOutput = open(os.path.join('.temp', 'allminutes.pdf'), 'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()
